package com.yue.gmall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yue.common.utils.PageUtils;
import com.yue.common.utils.Query;
import com.yue.gmall.product.dao.CategoryDao;
import com.yue.gmall.product.entity.CategoryEntity;
import com.yue.gmall.product.service.CategoryService;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<CategoryEntity> listWithTree() {
        List<CategoryEntity> entityList = baseMapper.selectList(null);
        //过滤出一级分类
        List<CategoryEntity> levelTopList =
                entityList.stream()
                        .filter((entity) -> {
                            return entity.getParentCid() == 0;
                        })
                        .peek((categoryEntity -> categoryEntity.setChildren(getChildren(categoryEntity, entityList))))
                        .sorted(Comparator.comparingInt(o -> (o.getSort() == null ? 0 : o.getSort())))
                        .collect(Collectors.toList());
        return levelTopList;
    }

    @Override
    public void removeMenuByIds(List<Long> ids) {
        //TODO 1. 检查当前要删除的菜单，是否被其他菜单引用
        // 2. 使用逻辑删除
        baseMapper.selectBatchIds(ids);
        baseMapper.deleteBatchIds(ids);
    }

    @Override
    public Long[] findCatelogPath(Long catelogId) {
        List<Long> paths = new ArrayList<>();
        paths = findParentPath(catelogId, paths);
        Collections.reverse(paths);

        return paths.toArray(new Long[paths.size()]);
    }

    private List<Long> findParentPath(Long catelogId, List<Long> paths) {
        paths.add(catelogId);
        CategoryEntity categoryEntity = this.getById(catelogId);
        if (0 != categoryEntity.getParentCid()) {
            findParentPath(categoryEntity.getParentCid(), paths);
        }

        return paths;
    }

    //设置子二级分类
    private List<CategoryEntity> getChildren(CategoryEntity root, List<CategoryEntity> all) {
        List<CategoryEntity> childrenList = all.stream()
                .filter((child) -> {
                    return child.getParentCid() == root.getCatId();
                })
                .peek((child) -> child.setChildren(getChildren(child, all)))
                .sorted(Comparator.comparingInt(o -> (o.getSort() == null ? 0 : o.getSort())))
                .collect(Collectors.toList());

        return childrenList;
    }

}